﻿var listid = "";
var arrlistid = "";
var num = 0;
var page = 1;
var keyword = ""; //关键词
var hannelid = ""; //频道id
var authid = ""; //作者id
var pagecount = "0";
var arrTitle = new Array();

Array.prototype.contains = function (obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

$(document).ready(function () {
    try {
        listid = getCookie("chanid");
        arrlistid = listid.split(',');
    }
    catch (e) { }


    loadhanne();            //加载频道
    load(keyword, page);    //加载列表
    loadShared();           //加载红包
    $("#Getimg").bind("click", qiang);
    $("#close").click(function () { $(".fixedHiden").css("display", "none"); $("#redshow").css("display", "none"); });
    $("#searchico").click(function () { srachkeyword() });

    $(window).bind("scroll", function () {
        //当滚动条滚动时
        var scollTop = $(this).scrollTop() + $(this).height();
        var scollH = $(document).height();
        if (scollH - scollTop < 100) {
            if (wait.isShow) { return; } //如果正在加载，则返回  
            page++;
            load(keyword, page);
        }
        return false;
    });

    if (getQueryString("a") != null) {
        SetCookie("a", getQueryString("a"));
        setSession("a", getQueryString("a"));
    }
});

function load(keyword, pages) {
    if (pagecount == "1") { return; }
    //$(".item_hide").html("");

    ajax.getAjax("Ajax/Article.ashx?GetListIndex", "channel_id=" + hannelid + "&author_id=" + authid + "&key_word=" + keyword + "&page_index=" + pages + "&is_recommend=1", function (data) {
        if (data.code == "4") { Alert("没有相关文章"); pagecount = "1"; return; }
        if (data.item.pageCount < pages) {
            if (pagecount == "0")
                Alert('到底了！');
            pagecount = "1"; return;
        }
        var max = 12;
        var phoneWidth = $("#wap").width();
        if (phoneWidth < 370)   //iphone5s=320
            max = 12;
        else if (phoneWidth > 370 && phoneWidth < 400)   //iphone6=375
            max = 16;
        else if (phoneWidth < 500)
            max = 22;
        else if (phoneWidth > 600)
            max = 28;


        var arr_index = 0;
        var arrId = new Array();
        var items = data.item.table;

        for (var i = items.length - 1; i >= 0; i--) {
            var titles = items[i].title == null ? "" : items[i].title;
            if (items[i].type == 2)
                titles = items[i].url_title == null ? "" : items[i].url_title;;
            //过滤重复的标题,保留最早的一条
            if (!arrTitle.contains(titles)) {
                arrId[arr_index] = items[i].id;
                arrTitle[arr_index] = titles;
                arr_index++;
            }
        }

        var itemHide = $(".item_hide");
        var itemCount = itemHide.length;
        var itemIndex = 0;

        $(data.item.table).each(function () {
            //默认图片
            var logoimg = this.logo == null ? "img/30.png" : this.logo;
            var titles = this.title == null ? "" : this.title;
            var click = this.click;
            var article_id = this.article_id;
            var advert_id = this.advert_id == null ? "" : this.advert_id;

            //外链
            if (this.type == 2) {
                click = this.shared_click;
                titles = this.url_title == null ? "" : this.url_title;;
                logoimg = this.url_logo;
            }

            if (arrId.contains(this.id)) {
                arrTitle[num] = titles;
                if (titles.length > max)
                    titles = titles.substr(0, max) + "...";

                if (itemIndex < itemCount) {
                    //用填充的DIV显示内容
                    itemHide.eq(itemIndex).html("<div class=\"centenlist\"><div class=\"centenimg\"><img src=\"" + logoimg + "\" /> </div> <div class=\"cententxt\"  onclick=\"alink('" + article_id + "','" + this.id + "','" + advert_id + "','" + this.type + "')\"><div id='t" + this.id + "' class='cententitlte'>" + titles + "</div><div class=\"centenTime\">" + this.date.substr(0, 10) + "</div></div></div>");
                    itemHide.eq(itemIndex).removeClass("item_hide");
                    itemIndex++;
                } else {
                    $("#ws_tg").append("<div class=\"centenlist\"><div class=\"centenimg\"><img src=\"" + logoimg + "\" /> </div> <div class=\"cententxt\"  onclick=\"alink('" + article_id + "','" + this.id + "','" + advert_id + "','" + this.type + "')\"><div id='t" + this.id + "' class='cententitlte'>" + titles + "</div><div class=\"centenTime\">" + this.date.substr(0, 10)
                    //+ "<div class=\"centenflow\"><img src=\"img/flow.jpg\" /> " + click + "</div>"//点击量
                    + "</div></div></div>");
                }
                num++;
            }
        });


        //alert(itemCount + " # " + arrId.length + " # " + itemHide.length);
        if (page < 3) {
            if (arrId.length < 12) {
                var length = 12 - arrId.length - itemHide.length;
                for (var i = 0; i < length; i++) {
                    $("#ws_tg").append("<div class='item_hide'><div class=\"centenlist\"></div></div>");
                }
            }
        }
    });
}

function srachkeyword() {

    pagecount = "0";
    var url = $("#Stext").val();
    var reg = new RegExp("[a-zA-z]+://[^\\s]*", "gi"); //匹配链接地址
    var result = url.match(reg);
    console.log(result);

    if (null == result || 0 == result.length) {
        Alert("无效的文章链接地址");
        return;
    }
    url = result[0];
    if (CheckUrl(url)) {
        if (url.indexOf("http") == -1) {
            url = "http://" + url;
        }

        if (url.indexOf(window.location.host) != -1) {
            toast.show("不允许分享内部网页");
            return;
        }

        if (url.indexOf("weixin.qq.com") > -1)
            url = url.replace("#wechat_redirect", "");  //处理微信链接不显示广告位
        $.post("/Client/Ajax/HttpSource.ashx?GetRawUrl", { url: url }, function (data) {
            wait.dismiss();
            console.log(data);
            if (data.code == 0) {
                url = data.item;
                window.location.href = "url-edit.html?rand=" + Math.random().toString().substring(2) + "&url=" + encodeURIComponent(url);
            }
            else {
                toast.show(data.msg);
            }

        }, "json");

    }
    else {
        page = 1;
        load(url, page);
    }
}

//注释06-02
function loadhanne() {
    //    ajax.getAjax("Ajax/Channel.ashx?GetList", "page_index=1", function (data) {
    //        var nums = 0;
    //        $("#swiper-wrapper").append("<div class=\"swiper-slide swiper-slide-color\" onclick=\"haneload('',this)\">全部</div>");
    //        $(data.item.table).each(function () {
    //            nums++;
    //            if (nums < 7) {
    //                $("#swiper-wrapper").append("<div class=\"swiper-slide\" onclick=\"haneload(" + this.id + ",this)\">" + this.name + "</div>");
    //            }
    //            else {
    //                if (isbool(this.id)) {
    //                    $("#swiper-wrapper").append("<div class=\"swiper-slide\"   onclick=\"haneload(" + this.id + ",this)\"> " + this.name + "</div>");
    //                }
    //            }
    //        });
    //        swiper();
    //    });
}
//频道加载列表
function haneload(id, obj) {
    hannelid = id;
    page = 1;
    $(".swiper-slide").removeClass("swiper-slide-color");
    $(obj).addClass("swiper-slide-color");
    $("#ws_tg").html("");
    pagecount = "0";
    load("", page);
}
//是否cookie记录的频道
function isbool(id) {
    if (arrlistid[0] != undefined) {
        for (var i = 0; i < arrlistid.length; i++) {
            if (id == arrlistid[i]) {
                return true;
            }
        }
    }
    return false;
}
function swiper() {
    var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        slidesPerView: 6, //显示个数
        paginationClickable: true,
        spaceBetween: 10, //间距
        freeMode: true
    });
}

//加载红包
function loadShared() {
    //客户端打开才显示红包
    var param = "";
    var arr = getCookie('area')
    if (arr != null && arr != '') {
        arr = arr.split(',');
        for (var i = 0; i < arr.length; i++) {
            if (i > 2) break;
            param += '&area' + (i + 1) + '=' + arr[i];
        }
    }
    ajax.getAjax("Ajax/fc_article.ashx?getlist_top", param, function (data) {
        var html = "";
        $(data.item.table).each(function () {
            var logoimg = this.logo == null ? "img/30.png" : this.logo;
            var titles = this.title;
            if (titles.length > 11)
                titles = titles.substr(0, 11) + "...";
            var imgrob = "<img class=\"rob\" src=\"img/Makeimg/newicon/hongbao.gif\" >";
            html += "<div class=\"centenlist\"><div class=\"centenimg\"><img src=\"" + logoimg + "\" /> </div> <div class=\"cententxt\"  onclick=\"hongbao(" + this.id + ")\"> " + imgrob + "<div id='title" + this.id + "' class='cententitlte'>" + titles + "</div><div class=\"centenTime\">" + this.date.substr(0, 10) + "<div class=\"centenflow\" style='text-align: center'>剩余:" + this.lock + "</div></div></div></div>";
        });
        if (html.length > 0) {
            $("#ws_tg").before(html);
            $("#tab3").append("<div class=\"newRed\"></div>");
        }
    });
}

//红包点击
function hongbao(id) {
    if (navigator.userAgent.indexOf("wjx") == -1) {
        if (getCookie("userid") == null || getCookie("userid") == "") {
            window.location.href = 'login.html?a=' + getQueryString("a");
            return;
        }
        //提示下载APP打开
        $("#bodytop").click();
        $(".wrap_detail").css("margin-left", parseInt($("#wap").css("width")) / 2 - 120);
        $(".wrap_detail").css("display", "");
        return;
    }


    //新的分享
    $("#redshow").css("display", "block");
    $(".fixedHiden").css("display", "block");
    $(".Titletxt").html($("#title" + id).text());
    $("#advrtid").val(id);
}

var q_state = false;
function qiang() {
    var article_id = $("#advrtid").val();
    if (article_id == "") {
        Alert("参数错误,抢红包失败!");
        return;
    }
    if (!checkHB(article_id)) {
        Alert("一台手机不能重复抢红包!");
        return;
    }
    if (q_state) {
        Alert("请稍等...");
        return;
    }
    ajax.getAjax("ajax/fc_shared_money.ashx?qiang", "&article_id=" + article_id, function (data) {
        q_state = false;
        if (data.code == 0) {
            $("#Getimg").bind("click", function () { Alert("正在跳转请稍等...") });
            window.location.href = "fc_detail.aspx?article_id=" + article_id + "&money_id=" + data.item;
        }
        Alert(data.msg);
    });
}

function alick(id) {
    //    if (IsMobile() == 1) {
    //        var ua = navigator.userAgent.toLocaleLowerCase();
    //        if (ua.indexOf("wjx") > -1) {
    //            var s = ua.substr(ua.indexOf("wjx"));
    //            var s2 = s.substr(3, s.indexOf("/") - 3);
    //            if (s2.indexOf("2.") > -1) {
    //                var param = { url: wjx_domain + "/client/to_detail.aspx?article_id=" + id, title: $("#t" + id).html(), hasTop: true, hasCopy: true, hasShare: true };
    //                window.location.href = "callback://openURL?" + JSON.stringify(param);
    //                return;
    //            }
    //        }
    //    }
    window.location.href = 'to_detail.aspx?article_id=' + id;
}

function alink(article_id, shared_id, advert_id, type) {
    //    if (IsMobile() == 1) {
    //        var ua = navigator.userAgent.toLocaleLowerCase();
    //        if (ua.indexOf("wjx") > -1) {
    //            var s = ua.substr(ua.indexOf("wjx"));
    //            var s2 = s.substr(3, s.indexOf("/") - 3);
    //            if (s2.indexOf("2.") > -1) {
    //                var param = { url: wjx_domain + "/client/to_detail.aspx?article_id=" + id, title: $("#t" + id).html(), hasTop: true, hasCopy: true, hasShare: true };
    //                window.location.href = "callback://openURL?" + JSON.stringify(param);
    //                return;
    //            }
    //        }
    //    }

    var url = "detail.aspx?is_app=1&shareid=" + shared_id + "&advert_id=" + advert_id;
    if (type == 2)
        url += "&article_id=0&type=2";
    else {
        url += "&article_id=" + article_id;
        //window.location.href = 'to_detail.aspx?article_id=' + article_id + "&user_id=" + uid;
    }
    window.location.href = url;
}

//拨打电话
function CellPhone() {
    $("#phone").removeClass("hide");
    $("#phone").css("display", "");
    var left = parseInt(($("#wap").width() - $(".phone").width()) / 2);
    $(".phone").css("left", left + "px")
}
